/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package counter.stosc.com;


import java.util.Vector;
import javax.microedition.lcdui.*;

/**
 *
 * @author Stosc
 */
class ListCounterUI extends List implements CommandListener{

    private Controller controller;
    private Counter[] counter;

    private Command exit = new Command("退出", Command.EXIT, 4);
    private Command edit = new Command("编辑", Command.ITEM, 2);
    private Command add  = new Command("添加",  Command.ITEM, 1);
    private Command remove = new Command("删除", Command.ITEM, 3);

    private static final String SPLIT_STRING = "_";

    public ListCounterUI(Controller controller) {
        super("修学计数器列表", List.IMPLICIT);
        this.controller = controller;
        addCommand(exit);
        addCommand(edit);
        addCommand(add);
        addCommand(remove);
        setCommandListener(this);
    }

    public void load(Counter[] counters) {
        // 首先清除以前的选项，便于刷新列表
        while(size()>0) {
            delete(0);
        }
        this.counter = counters;
        for(int i=0; i<counters.length; i++) {
            append(counters[i].getCounterName()+SPLIT_STRING+ String.valueOf(counters[i].getId()), null);
        }

    }

    public void commandAction(Command c, Displayable d) {
        int index = getSelectedIndex();
        int recordIndex = 0;
        if(index > 0){
            String [] strArray = split(this.getString(index), SPLIT_STRING);
            recordIndex = Integer.parseInt(strArray[strArray.length - 1]);
        }
//        if(recordIndex == (-1))
//            return;
        if(c==exit)
            controller.handleCommand(Controller.APP_EXIT, null);
        if(c==add)
            controller.handleCommand(Controller.SHOW_ADD_COUNTER, null);        
        if(c==edit && recordIndex != (-1))
            controller.handleCommand(Controller.SHOW_EDITZ_COUNTER, counter[recordIndex]);
        if(c==remove && recordIndex != (-1))
            controller.handleCommand(Controller.DO_REMOVE_COUNTER, counter[recordIndex]);
        if(c==SELECT_COMMAND && recordIndex != (-1))
            controller.handleCommand(Controller.SHOW_JOB_COUNTER, counter[recordIndex]);
    }

        /**
        * 分割字符串，原理：检测字符串中的分割字符串，然后取子串
        * @param original 需要分割的字符串
        * @paran regex 分割字符串
        * @return 分割后生成的字符串数组
        */
        private static String[] split(String original,String regex){
            int startIndex = 0;
            Vector v = new Vector();
            String[] str = null;
            int index = 0;

            startIndex = original.indexOf(regex);
            while(startIndex < original.length() && startIndex != -1){
                String temp = original.substring(index,startIndex);
                System.out.println(" " + startIndex);
                v.addElement(temp);
                index = startIndex + regex.length();
                startIndex = original.indexOf(regex,startIndex + regex.length());
            }

            v.addElement(original.substring(index + 1 - regex.length()));
            str = new String[v.size()];
            for(int i=0;i < v.size();i++){
                str[i] = (String)v.elementAt(i);
            }
            return str;
        }

}
